Conditions | 1 |
Paths | 16 |
Total Lines | 161 |
Lines | 0 |
Ratio | 0 % |
Changes | 0 |
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
1 | import { |
||
24 | export function getAsyncData({ |
||
25 | stateKey, dataSource, type, showTreeRootNode, extraParams = {} |
||
26 | }) { |
||
27 | |||
28 | return (dispatch) => { |
||
29 | |||
30 | dispatch(dismissEditor({ stateKey })); |
||
31 | |||
32 | dispatch( |
||
33 | setLoaderState({state: true, stateKey }) |
||
34 | ); |
||
35 | |||
36 | if (typeof dataSource === 'function') { |
||
37 | |||
38 | // passing extraParams.parentId |
||
39 | // to custom func so they can do partial |
||
40 | // loading |
||
41 | dataSource(extraParams).then((response) => { |
||
42 | |||
43 | if (response && response.data) { |
||
44 | |||
45 | dispatch( |
||
46 | setLoaderState({ state: false, stateKey }) |
||
47 | ); |
||
48 | |||
49 | if (type !== 'tree') { |
||
50 | |||
51 | dispatch({ |
||
52 | type: SET_DATA, |
||
53 | data: response.data, |
||
54 | total: response.total, |
||
55 | currentRecords: response.data, |
||
56 | success: true, |
||
57 | stateKey |
||
58 | }); |
||
59 | } |
||
60 | |||
61 | else { |
||
62 | // upon the return of read |
||
63 | // response needs to clarify |
||
64 | // whether this is a partial update |
||
65 | dispatch(setTreeData({ |
||
66 | data: response.data, |
||
67 | stateKey, |
||
68 | showTreeRootNode, |
||
69 | parentId: extraParams.parentId, |
||
70 | partial: response.partial |
||
71 | })); |
||
72 | } |
||
73 | |||
74 | return; |
||
75 | } |
||
76 | |||
77 | if (response && !response.data) { |
||
78 | /* eslint-disable no-console */ |
||
79 | console.warn( |
||
80 | `A response was recieved |
||
81 | but no data entry was found` |
||
82 | ); |
||
83 | console.warn( |
||
84 | `Please see |
||
85 | https://github.com/bencripps/react-redux-grid |
||
86 | for documentation` |
||
87 | ); |
||
88 | /* eslint-enable no-console */ |
||
89 | } |
||
90 | |||
91 | dispatch( |
||
92 | setLoaderState({ state: false, stateKey }) |
||
93 | ); |
||
94 | |||
95 | dispatch({ |
||
96 | type: ERROR_OCCURRED, |
||
97 | error: 'Unable to Retrieve Grid Data', |
||
98 | errorOccurred: true, |
||
99 | stateKey |
||
100 | }); |
||
101 | |||
102 | }); |
||
|
|||
103 | } |
||
104 | |||
105 | else if (typeof dataSource === 'string') { |
||
106 | |||
107 | if (type !== 'tree') { |
||
108 | |||
109 | return Request.api({ |
||
110 | route: dataSource, |
||
111 | method: 'GET' |
||
112 | }).then((response) => { |
||
113 | |||
114 | if (response && response.data) { |
||
115 | |||
116 | dispatch({ |
||
117 | type: SET_DATA, |
||
118 | data: response.data, |
||
119 | total: response.total, |
||
120 | currentRecords: response.data, |
||
121 | success: true, |
||
122 | stateKey |
||
123 | }); |
||
124 | |||
125 | } |
||
126 | |||
127 | else { |
||
128 | dispatch({ |
||
129 | type: ERROR_OCCURRED, |
||
130 | error: 'Unable to Retrieve Grid Data', |
||
131 | errorOccurred: true, |
||
132 | stateKey |
||
133 | }); |
||
134 | } |
||
135 | |||
136 | dispatch( |
||
137 | setLoaderState({state: false, stateKey }) |
||
138 | ); |
||
139 | }); |
||
140 | |||
141 | } |
||
142 | |||
143 | else { |
||
144 | |||
145 | return Request.api({ |
||
146 | route: dataSource, |
||
147 | method: 'GET', |
||
148 | queryStringParams: { |
||
149 | parentId: extraParams.parentId |
||
150 | } |
||
151 | }).then((response) => { |
||
152 | |||
153 | if (response && response.data) { |
||
154 | |||
155 | // response needs to specify |
||
156 | // whether this is full or partial update |
||
157 | dispatch(setTreeData({ |
||
158 | data: response.data, |
||
159 | stateKey, |
||
160 | showTreeRootNode, |
||
161 | partial: response.partial, |
||
162 | parentId: extraParams.parentId |
||
163 | })); |
||
164 | |||
165 | } |
||
166 | |||
167 | else { |
||
168 | dispatch({ |
||
169 | type: ERROR_OCCURRED, |
||
170 | error: 'Unable to Retrieve Grid Data', |
||
171 | errorOccurred: true, |
||
172 | stateKey |
||
173 | }); |
||
174 | } |
||
175 | |||
176 | dispatch( |
||
177 | setLoaderState({state: false, stateKey }) |
||
178 | ); |
||
179 | }); |
||
180 | } |
||
181 | } |
||
182 | |||
183 | }; |
||
184 | } |
||
185 | |||
430 |